package cn.wjdiankong.levideo.dialog;

import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.data.ClearCacheData;
import cn.wjdiankong.levideo.database.LocalStorage;
import cn.wjdiankong.levideo.list.adapter.ClearCacheListAdapter;
import cn.wjdiankong.levideo.list.adapter.ClearCacheListAdapter.ClickItemBtnListener;
import cn.wjdiankong.levideo.utils.Const;
import cn.wjdiankong.levideo.utils.FileUtils;
import cn.wjdiankong.levideo.utils.UIUtil;
import cn.wjdiankong.levideo.utils.Utils;
import cn.wjdiankong.levideo.view.LevideoDialog;

public class ClearCacheDialog {
	
	private Context mActivity;
	
	private View parentView;
	private LevideoDialog dialog;
	private ListView mListView;
	private List<ClearCacheData> cacheDataList;
	private ClearCacheListAdapter adapter;
	private TextView cacheTxt;
	private int douyinCacheSize, huoshanCacheSize, kuaishouCacheSize, miaopaiCacheSize, toutiaoCacheSize, imgCacheSize, totalCacheSize;
	
	@SuppressLint("InflateParams")
	public ClearCacheDialog(Activity act){
		mActivity = act;
		
		if(parentView == null){
			parentView = LayoutInflater.from(mActivity.getApplicationContext()).inflate(R.layout.dialog_clear_cache, null);
		}
		if(dialog == null){
			dialog = new LevideoDialog(mActivity, parentView, UIUtil.dip2px(mActivity.getApplicationContext(), 280), 0);
		}
		
		mListView = (ListView) parentView.findViewById(R.id.listview);
		Button clearBtn = (Button) parentView.findViewById(R.id.clear_btn);
		cacheTxt = (TextView) parentView.findViewById(R.id.cache_txt);
		clearBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean isSucc = FileUtils.deleteAllData();
				if(isSucc){
					LocalStorage.getInstance().deleteAllLevideoData();
				}
				if(mListener != null){
					mListener.clear(-2, totalCacheSize);
				}
				dialog.dismiss();
			}
		});
	}
	
	public ClearCacheDialog setDouyinCacheSize(int size){
		douyinCacheSize = size;
		return this;
	}
	
	public ClearCacheDialog setHuoshanCacheSize(int size){
		huoshanCacheSize = size;
		return this;
	}
	
	public ClearCacheDialog setKuaishouCacheSize(int size){
		kuaishouCacheSize = size;
		return this;
	}
	
	public ClearCacheDialog setMiaopaiCacheSize(int size){
		miaopaiCacheSize = size;
		return this;
	}
	
	public ClearCacheDialog setToutiaoCacheSize(int size){
		toutiaoCacheSize = size;
		return this;
	}
	
	public ClearCacheDialog setImageCacheSize(int size){
		imgCacheSize = size;
		return this;
	}
	
	public void show(){
		createDataList();
		if(adapter == null){
			adapter = new ClearCacheListAdapter(mActivity, cacheDataList);
		}
		adapter.setClickListener(new ClickItemBtnListener() {
			@Override
			public void click(int position) {
				clickClearBtn(position);
				dialog.dismiss();
			}
		});
		mListView.setAdapter(adapter);
		LayoutParams params = (LayoutParams) mListView.getLayoutParams();
		params.height = cacheDataList.size() * UIUtil.dip2px(mActivity.getApplicationContext(), 44);
		mListView.setLayoutParams(params);
		int videoSize = douyinCacheSize+huoshanCacheSize+miaopaiCacheSize+kuaishouCacheSize+toutiaoCacheSize;
		totalCacheSize = videoSize + imgCacheSize;
		cacheTxt.setText("视频缓存数据："+Utils.formatSize(videoSize)+"\n\n图片缓存数据："+Utils.formatSize(imgCacheSize));
		dialog.show();
	}
	
	private void clickClearBtn(int position){
		int clearCacheSize = 0;
		if(cacheDataList.get(position).type == -1){
			//图片
			FileUtils.deleteImgCacheData();
			imgCacheSize = 0;
			clearCacheSize = imgCacheSize;
		}else{
			boolean isSucc = FileUtils.deleteVideoData(cacheDataList.get(position).type);
			if(isSucc){
				LocalStorage.getInstance().deleteLevideoData(cacheDataList.get(position).type);
			}
			switch(cacheDataList.get(position).type){
				case Const.VIDEO_TYPE_DOUYIN:
					clearCacheSize = douyinCacheSize;
					douyinCacheSize = 0;
					break;
				case Const.VIDEO_TYPE_HUOSHAN:
					clearCacheSize = huoshanCacheSize;
					huoshanCacheSize = 0;
					break;
				case Const.VIDEO_TYPE_KUAISHOU:
					clearCacheSize = kuaishouCacheSize;
					kuaishouCacheSize = 0;
					break;
				case Const.VIDEO_TYPE_MIAOPAI:
					clearCacheSize = miaopaiCacheSize;
					miaopaiCacheSize = 0;
					break;
				case Const.VIDEO_TYPE_TOUTIAO:
					clearCacheSize = toutiaoCacheSize;
					toutiaoCacheSize = 0;
					break;
			}
		}
		
		if(mListener != null){
			mListener.clear(cacheDataList.get(position).type, clearCacheSize);
		}
		
		dialog.dismiss();
		
	}
	
	private void createDataList(){
		if(cacheDataList != null){
			cacheDataList.clear();
		}else{
			cacheDataList = new ArrayList<ClearCacheData>();
		}
		if(douyinCacheSize > 0){
			ClearCacheData douyinData = new ClearCacheData();
			douyinData.cacheSize = Utils.formatSize(douyinCacheSize);
			douyinData.name = "抖音";
			douyinData.type = Const.VIDEO_TYPE_DOUYIN;
			douyinData.logo = R.drawable.douyin;
			cacheDataList.add(douyinData);
		}
		if(kuaishouCacheSize > 0){
			ClearCacheData data = new ClearCacheData();
			data.cacheSize = Utils.formatSize(kuaishouCacheSize);
			data.name = "快手";
			data.type = Const.VIDEO_TYPE_KUAISHOU;
			data.logo = R.drawable.kuaishou;
			cacheDataList.add(data);
		}
		if(huoshanCacheSize > 0){
			ClearCacheData data = new ClearCacheData();
			data.cacheSize = Utils.formatSize(huoshanCacheSize);
			data.name = "火山";
			data.type = Const.VIDEO_TYPE_HUOSHAN;
			data.logo = R.drawable.huoshan;
			cacheDataList.add(data);
		}
		if(miaopaiCacheSize > 0){
			ClearCacheData data = new ClearCacheData();
			data.cacheSize = Utils.formatSize(miaopaiCacheSize);
			data.name = "秒拍";
			data.type = Const.VIDEO_TYPE_MIAOPAI;
			data.logo = R.drawable.miaopai;
			cacheDataList.add(data);
		}
		if(toutiaoCacheSize > 0){
			ClearCacheData data = new ClearCacheData();
			data.cacheSize = Utils.formatSize(toutiaoCacheSize);
			data.name = "头条";
			data.type = Const.VIDEO_TYPE_TOUTIAO;
			data.logo = R.drawable.slide_menu_toutiao_img;
			cacheDataList.add(data);
		}
		if(imgCacheSize > 0){
			ClearCacheData data = new ClearCacheData();
			data.cacheSize = Utils.formatSize(imgCacheSize);
			data.name = "缓存图片";
			data.type = -1;
			data.logo = -1;
			cacheDataList.add(data);
		}
	}
	
	public interface ClickClearListener{
		void clear(int type, int cacheSize);
	}
	
	private ClickClearListener mListener;
	public ClearCacheDialog setClickClearListener(ClickClearListener listener){
		mListener = listener;
		return this;
	}

}
